package Server;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
 
/**
 *
 * @author Elad
 */
public class MailServer {
	 
    private Session m_Session;
    private Message m_simpleMessage;
    private InternetAddress m_fromAddress;
    private Properties m_properties;
    
    public void sendMail(final String[] m_to,final String m_subject,final String m_body){
    	 
    	Thread mailThread=new Thread() {
    		public void run() {
    			
    			try {
    				   
    	              m_properties = new Properties();
    	              m_properties.put("mail.smtp.host", "smtp.gmail.com");
    	              m_properties.put("mail.smtp.socketFactory.port", "465");
    	              m_properties.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
    	              m_properties.put("mail.smtp.auth", "true");
    	              m_properties.put("mail.smtp.port", "465");
    	               
    	   
    	              m_Session        =   Session.getDefaultInstance(m_properties,new Authenticator() {
    	                  protected PasswordAuthentication getPasswordAuthentication() {
    	                      return new PasswordAuthentication("oblsystem2@gmail.com","obl$obl2"); // username and the password
    	                  }
    	   
    	              });
    	               
    	              m_simpleMessage  =   new MimeMessage(m_Session);
    	   
    	              m_fromAddress    =   new InternetAddress("oblsystem2@gmail.com");
    	              InternetAddress[] m_toAddress = new InternetAddress[m_to.length];
    	              for (int i = 0; i < m_to.length; i++)
    	              {
    	              	m_toAddress[i] = new InternetAddress(m_to[i]);
    	              }
    	              m_simpleMessage.setFrom(m_fromAddress);
    	              m_simpleMessage.setRecipients(RecipientType.TO, m_toAddress);
    	              m_simpleMessage.setSubject(m_subject);
    	              m_simpleMessage.setContent(m_body,"text/plain");
    	   
    	              Transport.send(m_simpleMessage);
    	   
    	          } catch (MessagingException ex) {
    	              ex.printStackTrace();
    	          }
    		}
		};
		mailThread.start();
        
      }
 
    public static void main(String[] args) {
    	 
        MailServer mail    =   new MailServer();
        String[] to= new String[] {"oblsystem2@gmail.com","eladkal@gmail.com"};
          mail.sendMail(to, "Test Mail", "Hi this is Test mail from Java Srilankan Support");
      }

}